import { setServant, getProtocol } from '../../api/index.js';

// pages/selectService/selectService.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    name: '',
    mobile: '',
    agree: false,
    protocol: '',
    showProtocol: false,
  },

  radioChange: function(e) {
    const {
      detail: { value },
    } = e;
    console.log(value);
    this.setData({
      type: value,
    });
  },

  navigate: function() {
    wx.navigateTo({
      url: '/pages/apply/apply',
    });
  },

  changeMeg: function(e) {
    const {
      currentTarget: {
        dataset: { name },
      },
      detail: { value },
    } = e.detail.event;
    console.log(e, 'dddd');

    if (name === 'serviceName') {
      this.setData({
        name: value,
      });
    }

    if (name === 'serviceMobile') {
      this.setData({
        mobile: value,
      });
    }
  },

  vaild: function() {
    const { name, mobile } = this.data;

    if (!name) {
      return wx.showToast({
        title: '请输入姓名',
        icon: 'none',
      });
    }

    if (!mobile) {
      return wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
      });
    }

    return true;
  },

  bindServant: function() {
    const that = this;
    let servantParams = {}
    const flag = this.vaild();
    const { name, mobile, agree, type } = this.data;
    const active = wx.getStorageSync('active')

    if (type === '1') {
      servantParams = { name, mobile, active }
    } else if (type === '2') {
      servantParams = { active }
    }

    if (!agree) {
      return wx.showToast({
        title: '请阅读并同意协议',
        icon: 'none',
      });
    }

    if(type === '1') {
      if (flag) {
        return setServant(servantParams)
          .then(res => {
            that.navigate();
          })
          .catch(e => {
            console.log(e, '绑定服务商');

            wx.showToast({
              title: e,
              icon: 'none',
            });
          });
      }
    }


    return setServant(servantParams)
      .then(res => {
        that.navigate();
      })
      .catch(e => {
        console.log(e, '绑定服务商');

        wx.showToast({
          title: e,
          icon: 'none',
        });
    });
  },

  onSubmit: function() {
    const { type, agree } = this.data;

    console.log(agree, '8888');

    if (!type) {
      return wx.showToast({
        title: '请选择服务商',
        icon: 'none',
      });
    }

    return this.bindServant();
  },

  bindAgree: function(e) {
    const {
      detail: { value },
    } = e;
    console.log(e, '8888 value');

    this.setData({
      agree: !this.data.agree,
    });
  },

  getProtocol: function() {
    getProtocol({})
      .then(res => {
        this.setData({
          showProtocol: true,
          protocol: res.data.protocol,
        });
        console.log(res, '获取协议');
      })
      .catch(e => {
        console.log(e, '协议');
      });
  },

  hideModal: function() {
    this.setData({
      showProtocol: false,
    });
  },
});
